<template>
  <div class="templateCss">
    <div>
      <a-row class="rigBox1" id="target1" ref="dataInfo">
        <a-col :span="1" class="hei56"></a-col>
        <a-col :span="17" class="hei56">
          <a-form :form="form1" :label-col="{ span: 7 }" :wrapper-col="{ span: 12 }">
            <a-form-item label="模板标题">
              <a-input
                :disabled="status ? true : false"
                v-decorator="['name', { rules: [{ required: true, message: '请输入模板标题!' }] }]"
              />
              <div class="form1Class">*标题仅做管理使用，不在员工填写页面展示</div>
            </a-form-item>
          </a-form>
        </a-col>
        <a-col :span="6"></a-col>
      </a-row>
    </div>
    <div>
      <a-form :form="form" :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
        <Draggable v-model="sortedKeys" group="form-draggable" animation="300" @add="onDragAdd" @end="onDragEnd">
          <div v-for="item in sortedKeys" :key="item" style="cursor: move">
            <div>
              <a-row class="rigBox" style="">
                <a-col :span="1" class="hei56">
                  <a-icon class="tableIcon" type="unordered-list" />
                </a-col>
                <a-col :span="21" class="hei56">
                  <div v-if="formFields[item]" style="display: flex; align-items: center">
                    <div style="margin-top: 9px; flex: 1">
                      <XqItem
                        ref="XqItem"
                        :form="form"
                        :dataSource="dataSource"
                        :item="formFields[item]"
                        :dynamicData="dynamicData"
                        :editable="editable"
                        @change="XqItemChange($event, formFields[item], dataSource, dynamicData)"
                      />
                    </div>
                    <div v-if="!formFields[item].isNecessary" class="dn" style="width: 70px; flex-shrink: 0">
                      <a-checkbox
                        v-model="formFields[item].decorator.rules[0].required"
                        @change="(e) => handleChange(e.target.value, item)"
                      >
                        必填
                      </a-checkbox>
                    </div>
                    <div v-else style="width: 70px; height: 20px; flex-shrink: 0"></div>
                  </div>
                </a-col>
                <a-col :span="2" class="parelative">
                  <div class="paAbsolut" v-if="formFields[item] && !formFields[item].isRequired">
                    <a-icon type="close" @click="delClose(item)" class="tableIcon1 dn" />
                  </div>
                </a-col>
              </a-row>
            </div>
          </div>
        </Draggable>
      </a-form>
    </div>
    <div style="height: 60px"></div>
    <div class="saveShow" :style="{ left: fixedLeft }">
      <a-space>
        <a-button type="primary" @click="save">保 存</a-button>
      </a-space>
    </div>
  </div>
</template>
<script>
import Draggable from "vuedraggable";
import XqFormMixin from "xqjr-plugin-form/mixins/XqFormMixin";
const FormMixin = XqFormMixin();
export default {
  components: { Draggable },
  mixins: [FormMixin],
  props: {
    sortedKeysNew: {
      type: Array,
      default: () => null
    },
    status: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ""
    },
    remove: {
      type: Boolean,
      default: false
    },
    dataSource: {
      type: Object,
      default: () => {}
    },
    editable: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: true
    },
    dynamicData: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    fixedLeft() {
      const isOpened = this.$store.state.app.sidebar.opened;
      return `${(isOpened ? 210 : 64) + 24 + 360}px`;
    }
  },
  watch: {
    sortedKeysNew: {
      handler(val) {
        this.sortedKeys = val;
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      scrollTop: 0,
      sortedKeys: [],
      form1: this.$form.createForm(this, { name: "coordinated" }),
      reportList: ["12", "34", "23"],
      isReward: true,
      spouseInfo: false
    };
  },
  created() {
    const data = { name: this.name };
    this.$nextTick(() => {
      this.form1.setFieldsValue(data);
    });
  },
  methods: {
    onDragEnd(event) {
      this.$emit("dragEnd", event.newIndex, event.oldIndex);
    },
    onDragAdd(event) {
      this.$emit("dragAdd", event.newIndex);
    },
    handleChange(e, test) {
      this.$emit("changItemVal", test, "updata");
    },
    delClose(e) {
      this.$emit("changItemVal", e, "del");
    },
    XqItemChange(val, item, dataSource, dynamicData) {
      if (item.type === "select_add") {
        this.selectAddValueDict[item.model] = val;
      } else if (item.model === "rewardPunish") {
        console.log("debug log --> ", this.form.resetFields());
        this.isReward = false;
        this.form.rewardType = null;
        this.$emit("itemChange", val);
        this.isReward = true;
      }
    },
    save() {
      this.form1.validateFields((err, values) => {
        if (!err) {
          this.$emit("change", values, this.sortedKeys); // 排序的
        } else {
          document.querySelector("#target1").scrollIntoView();
          this.$emit("scrollTop");
        }
      });
    },
    onRemove() {
      this.$emit("remove");
    },
    cancel() {
      this.$emit("update:editable", false);
      this.$emit("cancel");
    },
    onValuesChangeCustom(values) {}
  }
};
</script>
<style lang="scss">
.templateCss {
  overflow-y: auto;
  .ant-calendar-picker {
    width: 100%;
  }
  .ant-form-item-label > label {
    font-size: 14px;
    font-weight: 400;
    color: #55565d;
  }
  .ant-checkbox + span {
    font-weight: 400;
    color: #55565d;
    font-size: 14px;
  }
  .ant-form-item {
    margin-bottom: 0;
  }
  .hei56 {
    min-height: 56px;
    line-height: 56px;
  }
}
</style>
<style lang="scss" scoped>
.DynamicForm {
  text-align: center;
}
.saveShow {
  height: 60px;
  display: flex;
  position: fixed;
  bottom: 24px;
  right: 24px;
  align-items: center;
  justify-content: center;
  background: rgb(255, 255, 255);
  box-shadow: -1px 0px 4px 0px rgba(0, 0, 0, 0.13);
  z-index: 1;
}
.form1Class {
  font-size: 12px;
  font-family: SourceHanSansCN-Regular, SourceHanSansCN;
  color: #a6a8b4;
  text-align: left;
}
.rigBox {
  margin: 0 24px;
  border-radius: 4px;
  padding: 0 0 14px 14px;
  padding-bottom: 4px;
}
.rigBox1 {
  margin: 0 24px;
  border-radius: 4px;
  padding: 0 0 14px 14px;
}
.parelative {
  position: relative;
}
.paAbsolut {
  position: absolute;
  top: 0;
  right: 0;
}
.tableIcon {
  width: 13px;
  height: 11px;
  color: var(--primary);
  margin-right: 8px;
}
.tableIcon1 {
  font-size: 13px;
  width: 24px;
  height: 24px;
  background: var(--primary);
  border-radius: 0px 4px 0px 4px;
  color: #fff;
  line-height: 24px;
}
.dn {
  display: inline-block;
  visibility: hidden;
  transition: all 0s;
}
.rigBox:hover {
  background: #ffffff;
  box-shadow: 0px 4px 12px 0px rgba(106, 102, 246, 0.1);
  border: 1px dashed var(--primary);

  .dn {
    display: inline-block;
    visibility: visible;
  }
}
</style>
